package adapter;

import java.util.ArrayList;

import db.AdminBaseDatosHelper;
import lfm.mpr.JugadoresEquipoActivity_3_4_3;
import lfm.mpr.JugadoresEquipoActivity_4_3_3;
import lfm.mpr.JugadoresEquipoActivity_4_4_2;
import lfm.mpr.MyTeamsLFMActivity;
import lfm.mpr.R;
import lfm.mpr.TabHost_Liga;
import lfm.mpr.TeamTabsActivity;
import Soap.Connection_Sever_Dynamics;
import Toast.ToastLFM_Message;
import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import objects.Equipo;
import objects.Liga;
import objects.User;

public class AdapterListMyLigas extends ArrayAdapter<Liga>{

	private ArrayList<Liga> list_liga;
	private static Context context;
	private static Liga liga;
	private static AdminBaseDatosHelper miBBDDHelper;
	private boolean[] itemSelection;

	public AdapterListMyLigas(Context context, ArrayList<Liga> list) {
		super(context, R.layout.list_view_myligas, list);
		this.context = context;
        this.list_liga = list;
        this.miBBDDHelper = new AdminBaseDatosHelper(context);
        this.itemSelection = new boolean[list.size()];
	}//end
	
	static class ViewHolder {
        protected TextView name, jornadas, estado;
        static CheckBox check;
    }
	  
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
        final ViewHolder viewHolder;
        
        if (convertView == null) {
        	
        	viewHolder = new ViewHolder();
        	LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	
	        convertView  = inflater.inflate(R.layout.list_view_myligas, null);
	            
	        viewHolder.name = (TextView) convertView.findViewById(R.id.text_name_liga);
	        viewHolder.jornadas = (TextView) convertView.findViewById(R.id.textView_jornadas);
	        viewHolder.estado = (TextView) convertView.findViewById(R.id.textView_estado);
	        viewHolder.check = (CheckBox)  convertView.findViewById(R.id.checkBox_liga);
	        viewHolder.check.setTag(itemSelection[position]);
            convertView.setTag(viewHolder);

        } else {
        	viewHolder = (ViewHolder) convertView.getTag();
        }
	        viewHolder.name.setText(this.list_liga.get(position).getNombre());
	        viewHolder.jornadas.setText(new Integer(this.list_liga.get(position).getJornadas()).toString() + " jornadas");
	        viewHolder.estado.setText(this.list_liga.get(position).getEstado());
	        
	        if(this.list_liga.get(position).getEstado().equals("Pendiente")) {
	        	viewHolder.estado.setTextColor(Color.parseColor("#ff0000"));//rojo
	        }
	        else viewHolder.estado.setTextColor(Color.parseColor("#0000FF"));//verde
	        
	        viewHolder.check.setTag(itemSelection[position]);

	        
	        viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	            public void onCheckedChanged(CompoundButton buttonView,
	                    boolean isChecked) { 
	            	
	            	
	            	if(isChecked) {
	            		if(validate_selected_item(itemSelection)) {
	            			itemSelection[position] = true;
		            		liga = list_liga.get(position);
		            		((Activity) context).openOptionsMenu();
		            		}
	            		else {
	            			buttonView.setChecked(false);
	            			//viewHolder.check.setChecked(false);
	            			ToastLFM_Message.Show_Alert("Aviso", "Solo puedes seleccionar una liga", context);
	            		}
	            	}
	            		else {
	            			itemSelection[position] = false;
	            			liga = null;
	            			((Activity) context).closeOptionsMenu();

	            		}
	            }//end_onCheckedChanged
	        });
	        
	        convertView.setOnClickListener( new View.OnClickListener(){
                public void onClick(View v){
                	liga = (Liga) list_liga.get(position);
                	Intent intent = new Intent(context, TabHost_Liga.class);
                	intent.putExtra("ligaid", liga.getLiga_Id());
                	intent.putExtra("liga", liga.getNombre());
                	intent.putExtra("ligastatus", liga.getEstado());
	        		context.startActivity(intent);
                	//ToastLFM_Message.Show_Alert("Aviso", "Detalle Liga", context);
                        }
                });//end_listView_ali.setOnItemClickListener
	        
        return convertView;
        }
	
	public static Liga Delete_Liga() throws Exception{
		
		try {
			if(liga == null) ToastLFM_Message.Show_Alert("Aviso", "Debes seleccionar un Equipo", context);
			else {
				Log.i("LFM_Log","ID DE la liga " + liga.getLiga_Id());
				if(liga.getLiga_Id() == null){
					Log.i("LFM_Log","BORRAMOS EL EQUIPO SOLO DE LA BD");
					miBBDDHelper.Remove_Liga_LFM_BD(liga);
				}
				else {
					Log.i("LFM_Log","BORRAMOS la liga  DE LA BD Y EN EL SERVIDOR");
					Connection_Sever_Dynamics.Delete_Liga_LFM(liga.getLiga_Id());
					miBBDDHelper.Remove_Liga_LFM_BD(liga);
				}
				ViewHolder.check.setChecked(false);
			}
		} catch (Exception e) {
			throw e;
		}
       return liga;
	}//end_Delete_Team
	
	public static Liga Selected_Liga() {
       return liga;
	}//end_Delete_Team
	
	private boolean validate_selected_item(boolean[] itemSelection){
		
		boolean result = true;
		
		for (int i = 0; i < itemSelection.length; i++) {
			if(itemSelection[i]) return false;
		}
		
		return true;
	}
	
	
	
    }//end_class
